package com.qidian.study.dao.impl;

import com.qidian.study.dao.ContentCatDAO;
import com.qidian.study.pojo.Content;
import com.qidian.study.pojo.ContentCat;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 */
public class ContentCatDAOImpl extends BaseDAO implements ContentCatDAO {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public int save(ContentCat cat) {
        String sql = "insert into v_content_cat(cat_name,cat_desc,create_time,modify_time,status) " +
                " values(?,?,?,?,?)";
        return executeUpdate(sql,cat.getCatName(),cat.getCatDesc(),cat.getCreateTime(),cat.getModifyTime(),cat.getStatus());
    }

    @Override
    public int update(ContentCat cat) {
        String sql = "update v_content_cat set cat_name=?,cat_desc=?,modify_time=?,status=? " +
                " where cat_id = ?";
        return executeUpdate(sql,cat.getCatName(),cat.getCatDesc(),cat.getModifyTime(),cat.getStatus(),cat.getCatId());
    }

    @Override
    public ContentCat queryById(Long catId) {
        ContentCat cat = null;
        String sql = "select * from v_content_cat where cat_id = ?";
        try{
            con = getCon();
            pst = con.prepareStatement(sql);
            pst.setLong(1,catId);
            rs = pst.executeQuery();
            if(rs.next()){
                cat = new ContentCat();
                cat.setCatId(rs.getLong("cat_id"));
                cat.setCatName(rs.getString("cat_name"));
                cat.setCatDesc(rs.getString("cat_desc"));
                cat.setCreateTime(rs.getString("create_time"));
                cat.setModifyTime(rs.getString("modify_time"));
                cat.setStatus(rs.getInt("status"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return cat;
    }

    @Override
    public ContentCat queryByName(String catName) {
        ContentCat cat = null;
        String sql = "select * from v_content_cat where cat_name = ?";
        try{
            con = getCon();
            pst = con.prepareStatement(sql);
            pst.setString(1,catName);
            rs = pst.executeQuery();
            if(rs.next()){
                cat = new ContentCat();
                cat.setCatId(rs.getLong("cat_id"));
                cat.setCatName(rs.getString("cat_name"));
                cat.setCatDesc(rs.getString("cat_desc"));
                cat.setCreateTime(rs.getString("create_time"));
                cat.setModifyTime(rs.getString("modify_time"));
                cat.setStatus(rs.getInt("status"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return cat;
    }

    @Override
    public List<ContentCat> queryAll() {
        List<ContentCat> cs = new ArrayList<>();
        String sql = "select * from v_content_cat";
        try{
            con = getCon();
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            while(rs.next()){
                ContentCat cat = new ContentCat();
                cat.setCatId(rs.getLong("cat_id"));
                cat.setCatName(rs.getString("cat_name"));
                cat.setCatDesc(rs.getString("cat_desc"));
                cat.setCreateTime(rs.getString("create_time"));
                cat.setModifyTime(rs.getString("modify_time"));
                cat.setStatus(rs.getInt("status"));
                cs.add(cat);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pst,con);
        }
        return cs;
    }
}
